/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DB;

import Model.Camera;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;



/**
 *
 * @author Selemani
 */
public class CameraManager implements IFCameraManager {
    
    //SQL queries
    private static final String GET_ALL_Cameras = "SELECT * FROM camera";
    private static final String SAVE_Camera = "INSERT INTO Camera"
            + " VALUES(?,?,?,?,?,GETDATE(),?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
    
    
    private static final String UPDATE_Camera = "UPDATE Camera SET serialNumber=?,"
            + "alias=?, type=?, ip=?, port=?, lastUpdated=?, posXMax=?, posXMin=?,posYMax=?, posZMin=?,posZMax=?,tiltPosMax=?,tiltPosMin=?,slidePosMax=?,slidePosMin=?,cranePosXMax=?,cranePosXMin=?,cranePosYMax=?,cranePosYMin=? WHERE serialNumber=?";
   
    private static final String DELETE_Camera = "DELETE FROM camera"
            + " WHERE serialNumber = ?";
    
    //Prepared statements
    private PreparedStatement psGetAllcameras;
    private PreparedStatement psSavecamera; 
    private PreparedStatement psUpdateCamera;
    private PreparedStatement psDeleteCamera;  
      //Connection
     private Connection conn;
    
     

    public CameraManager(){
        conn = DBConnector.getInstance().getDBConnection();
        initialiseStatements();
    }
    
    /**
     * Get cameras
     * @return
     * @throws IllegalArgumentException 
     */    
    @Override
    public ArrayList<Camera> getCameras() throws IllegalArgumentException {
        ArrayList<Camera> allSc = new ArrayList<Camera>();
        Camera curr;
        try {
            ResultSet results = psGetAllcameras.executeQuery();
            while(results.next()){
                curr = new Camera();
                curr.setSerialNumber(results.getInt("serialNumber"));
                curr.setAlias(results.getNString("alias"));
                curr.setType(results.getNString("type"));
                curr.setIp(results.getNString("ip"));
                curr.setPort(results.getInt("port"));
                curr.setLastUpdated(results.getDate("lastUpdate"));
                curr.setPosXMax(results.getInt("posXMax"));  
                curr.setPosXMin(results.getInt("posXMin")); 
                curr.setPosYMax(results.getInt("posYMax"));  
                curr.setPosYMin(results.getInt("posYMin")); 
                curr.setPosZMax(results.getInt("posZMax"));  
                curr.setPosZMin(results.getInt("posZMin"));                    
                curr.setTiltPosMax(results.getInt("tilPosMax")); 
                curr.setSlidePosMin(results.getInt("tilPosMin")); 
                curr.setSlidePosMax(results.getInt("slidePosMax")); 
                curr.setSlidePosMin(results.getInt("slidePosMin"));                 
                curr.setCranePosXMax(results.getInt("cranePosXMax"));
                curr.setCranePosXMin(results.getInt("cranePosXMin"));
                curr.setCranePosYMax(results.getInt("cranePosYMax"));
                curr.setCranePosYMin(results.getInt("cranePosYMin"));
                             
                allSc.add(curr);
            }
        } catch (SQLException ex) {
            System.err.println(ex);
        }
        if(allSc.isEmpty()){
            throw new IllegalArgumentException("No cameras");
        }
        return allSc;
    }

   /**
     * Save camera
     * @param c
     * @throws IllegalArgumentException 
     */

    @Override
    public void saveCamera(Camera c)throws IllegalArgumentException {
             PreparedStatement toExc;
        
        int sNumber = c.getSerialNumber();
        try {
            if(sNumber == 0){
                 toExc = psSavecamera;
            }else{
                toExc = psUpdateCamera;
                toExc.setInt(20, c.getSerialNumber());
            }
                    
               
                toExc.setInt(1,c.getSerialNumber());
                toExc.setString(2, c.getAlias());
                toExc.setString(3,c.getAlias());
                toExc.setString(4, c.getType());
                toExc.setString(5,c.getIp());
                toExc.setInt(6, c.getPort());       
                toExc.setInt(8, c.getPosXMax());
                toExc.setInt(9,c.getPosXMin());
                toExc.setInt(10, c.getPosYMax());
                toExc.setInt(11,c.getPosYMin());
                toExc.setInt(12, c.getPosZMax());
                toExc.setInt(13,c.getPosZMin());
                toExc.setInt(13, c.getTiltPosMax());
                toExc.setInt(14,c.getTiltPosMin());
                toExc.setInt(15, c.getSlidePosMax());
                toExc.setInt(16,c.getSlidePosMin());
                toExc.setInt(17, c.getCranePosXMax());
                toExc.setInt(18,c.getCranePosXMin());
                toExc.setInt(19, c.getCranePosYMax());
                toExc.setInt(20,c.getCranePosYMin());
               
                
                int resultCode = toExc.executeUpdate();
                
           if(resultCode != 0 && sNumber == 0){
                c.setSerialNumber(resultCode);
               
               
            }else if(resultCode == 0){
                throw new IllegalArgumentException("Failed to save camera");
            }
        } catch (SQLException ex) {
            System.err.println(ex);
        }
        
        
    }
        
       /**
     * Delete camera
     * @param c
     * @throws IllegalArgumentException 
     */  
	  

    @Override
    public void deleteCamera(Camera c) throws IllegalArgumentException {
       
       try {
            psDeleteCamera.setInt(1, c.getSerialNumber());
            if(psDeleteCamera.executeUpdate() == 0){
                throw new IllegalArgumentException("Camera doesn't exist");
            }
        } catch (SQLException ex) {
           System.err.print(ex);
        }
    }
   
   
    
    
    private void initialiseStatements() {
        try {
            psGetAllcameras = conn.prepareStatement(GET_ALL_Cameras );
            psSavecamera = conn.prepareStatement(SAVE_Camera);
            psUpdateCamera = conn.prepareStatement(UPDATE_Camera);
            psDeleteCamera = conn.prepareStatement(DELETE_Camera);
        } catch (SQLException ex) {
        }
    }
/**
     * Get camera 
     * @param serialNumber
     * @return
     * @throws IllegalArgumentException 
     */
    @Override
    public Camera getCamera(int serialNumber) throws IllegalArgumentException {
        //Definitely needs to be rewriten
        for(Camera camera: this.getCameras()){
            if(camera.getSerialNumber() == serialNumber){
                return camera;
            }
                
        }
        return null;
    }
         
}
